;(function($) {
	var mainObj = {}, para = {
		ww : "",
		wh : "",
		muh : "",
		muh1 : "",
		muh2 : "",
		sr1 : "",
		sr2 : "",
		sr3 : "",
		i : "",
		ml : "",
		proph : "",
		vn1 : "",
		vn2 : "",
		vn3 : ""
	};
	mainObj.init = function() {
		mainObj.events();
	}

	mainObj.events = function() {
		$(".nav_left").each(
				function(index, element) {
					/* 左侧一级导航切换 */
					$(this).bind("click", function() {
						para.vn1 = $(this).find("img").attr("src");
						para.vn2 = para.vn1.substr(12, 1); // 获取src中数字
							para.vn3 = parseFloat(para.vn2); // string->number
							para.ml = $(".menu_first").children("a").length;
							para.sr1 = "images/menu_" + (para.vn3) + "_on.png";
							for (para.i = 0; para.i < para.ml; para.i++) {
								para.sr2 = $(".nav_left").eq(para.i)
										.find("img").attr("src");
								para.sr3 = para.sr2.substr(0, 13) + ".png";
								$(".nav_left").eq(para.i).find("img").attr(
										"src", para.sr3);
							}
							$(this).siblings().removeClass("nav_left_on");
							$(this).addClass("nav_left_on");
							$(this).find("img").attr("src", para.sr1);
							$(".list_a").removeClass("list_a_on");
							$(".ul_menu").eq(index).show().siblings("ul")
									.hide();
						})
				});
		$(".list_a").each(
				function(index, element) {
					/* 二级菜单切换 */
					$(this).click(
							function() {
								$(this).addClass("list_a_on").parent("li")
										.siblings().find("a").removeClass(
												"list_a_on");
							})
				});
		$(".close_icon,.cancel_btn").click(function() {
			/* 弹框关闭按钮 */
			$(this).parent().parent().hide();
			$(".opacity_div").hide();
		});

		$(".right_content").delegate(".cancel_btn,.close_infobtn", "click",
				function() {
					/* 取消创建角色 */
					$(this).parents(".right_content").hide();
					$(".search_div").show().siblings(".turn_page").show();
				});
		$(".right_content").delegate(".sure_btn", "click", function() {
			/* 确定创建角色 */
			$(".opacity_div").show();
			$(".small_prop").show();
		});
		$(".small_prop").delegate(".btn_pop", "click", function() {
			/* 小弹框点击确定后弹框消失 */
			$(".opacity_div").hide();
			$(".small_prop").hide();
		});
		$(".drop_frame i").each(function(){
			$(this).attr("title",$(this).text());
		});
		// 点击选择状态，显示下拉框
		$(".drop_frame").click(function(e) {
			/*$(this).addClass('selected');
			$(".choose_ul").hide();
			// alert($(this).find(".choose_a").);
				$(this).children(".choose_ul").show();*/
			
			if($(this).children(".choose_ul").is(':visible')){
				$(this).children(".choose_ul").slideUp("fast");
				$(this).removeClass('selected');
			}else{
				//console.log($(e.target).prop("nodeName"));
				if(!($(e.target).prop("nodeName").toLowerCase() =="a")){
					$(this).children(".choose_ul").slideDown("fast");
				}
				
				$(this).removeClass('selected');
			}	
			$(".choose_ul").not($(this).children(".choose_ul")[0]).slideUp("fast");
			$(".drop_frame").not(this).removeClass('selected');
		});
		
		
		/* 点击页面其他地方隐藏下拉框 */
		$(document)
				.bind(
						"click",
						function(e) {
							var target = $(e.target);
							if (target.closest(".drop_frame").length == 0
									|| e.target.className == "choose_a") {
								$(".choose_ul").slideUp("fast").parent().removeClass(
										'selected');
							}
						});
		/* 点击下拉列表中某一项，对应改变下拉框的内容 */
		$('body').delegate(
				'.choose_a',
				'click',
				function() {
					$(this).parents(".drop_frame").children(".text_i").text(
							$(this).text());
					$(this).parents("ul").find("a").removeAttr("selected");
					$(this).attr("selected","selected");
					$(this).parents(".drop_frame").children(".text_i").attr("title",$(this).text());
					var status = $(this).attr("data-status");
					$(this).parents(".drop_frame").children(".text_i").attr(
							"data-status", status);
					$(this).parents(".drop_frame").children("input[type='hidden']").val(status);
				});

		$(".first_check").change(
				function() {
					/* 复选框全选和取消全选 */
					$(".check_td").children('input[name="subBox"]').attr(
							"checked", this.checked);

				});
		var $subBox = $("input[name='subBox']");
		$subBox.click(function() {
					/* 所有复选框都选中时，全选框也同时勾上 */
					$(".first_check").attr("checked",$subBox.length == $("input[name='subBox']:checked").length ? true
											: false);
				});
		
		
	}
	mainObj.init();
	
	
	$("#firstPage").on("click",function(){
		yema=1;
		qryList();		
	});
	$("#lastPage").on("click",function(){
		yema=totalPage;
		qryList();
	});
	$("#prev").on("click",function(){
		if(yema>1){
			yema=yema-1;
			qryList();
		}
	});
	$("#next").on("click",function(){
		if(yema<totalPage){
			yema=yema+1;
			qryList();
		}
	});
	$("#go_num_input").on("click",function(){
		var page_num = $("#page_num_input").val();
		if(page_num == ""){
			LQWarn("请先输入页码");
			return;
		}
		if(page_num-1<totalPage){
			yema=parseInt(page_num);
			qryList();
		}else{
			LQWarn("不能超出总页数");
		}
	});
	
})(jQuery)

function showTips(tipText, time) {
	var tipsDiv = '<div class="tip_dialog hide"><div class="tipDialog_box"><p>'+tipText+'</p></div></div>';
	$('body').append(tipsDiv);
	var elem = $(".tip_dialog"),t = time || 2000,len;
	elem.find('p')
	.html(""+tipText);
	elem.fadeIn();
	setTimeout(function(){elem.fadeOut();},t);
	setTimeout(function(){elem.remove();},t);
}
function format(timeStamp){
	if(timeStamp==undefined){
		return "";
	}
	//timeStamp是整数，否则要parseInt转换
	var time = new Date(timeStamp);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y+'-'+(m<10?'0'+m:m)+'-'+(d<10?'0'+d:d)+' '+(h<10?'0'+h:h)+':'+(mm<10?'0'+mm:mm)+':'+(s<10?'0'+s:s);
}
function formatToDate(timeStamp){
	if(timeStamp==undefined || timeStamp == null || timeStamp == ""){
		return "";
	}
	//timeStamp是整数，否则要parseInt转换
	var time = new Date(timeStamp);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	
	return y+'-'+(m<10?'0'+m:m)+'-'+(d<10?'0'+d:d);
}

function formatToDateTime(timeStamp){
	if(timeStamp==undefined || timeStamp == null || timeStamp == "" ){
		return "";
	}
	//timeStamp是整数，否则要parseInt转换
	var time = new Date(timeStamp);
	var y = time.getFullYear();
	var m = time.getMonth()+1;
	var d = time.getDate();
	var h = time.getHours();
	var mm = time.getMinutes();
	var s = time.getSeconds();
	return y+'-'+(m<10?'0'+m:m)+'-'+(d<10?'0'+d:d)+' '+(h<10?'0'+h:h)+':'+(mm<10?'0'+mm:mm)+':'+(s<10?'0'+s:s);
}
function replaceNull(obj){
	if(obj==null||obj==""||obj=="undifine"){
		return "";
	}else{
		return obj;
	}
}

function strToJson(str){
	if(!str || str == "") return null;
	var obj = eval('(' + str + ')');
	return obj;
}
function isEmpty(obj){
	if(obj==null||obj==""){
		return true;
	}
	return false;
}


function clearForm(obj){
	obj.find("input[type='text']").val("");
	obj.find("textarea").val("");
	obj.find("input[type='radio']").prop("checked",false);
	obj.find("input[type='checkbox']").prop("checked",false);
	obj.find("img").prop("src","");
	obj.find(".imgPreview").html("");
	obj.find(".text_i").attr("data-status","0").html("请选择").attr("title","请选择");
}

function checkhaveNull(obj){
	var have_null = false;
	var value = "";
	var text_ ;
	var msg = "";
	var nodeName=null;
	var name =null;
	obj.find(".notNull").each(function(){
		value = "";

		 nodeName = $(this).prop("nodeName");
		var type = $(this).attr("type");
		//console.log(nodeName);
		if(type && type == "text"){
			value = $(this).val();
			text_ = $(this).prev("label").text();
			msg = text_.substring(0,text_.length-1)+'不能为空!';
		}else if(type && type == "hidden"){
			value = $(this).val();
			text_ = $(this).attr("data-name");
			msg = text_+'不能为空!';
		}else if(type && type == "checkbox"){
			name = $(this).attr("name");
			
			 $(this).parent().children("input[name='"+name+"']:checked").each(function(i,dom){
			 	value += dom.value;
			 });
			text_ = $(this).prevAll("label").eq(0).text();
			if(!text_){
					text_ = $(this).parent().prevAll("label").eq(0).text();
			}
			msg = text_.substring(0,text_.length-1)+'不能为空!';
		}
		else if($(this).hasClass("text_i")){
			value = $(this).attr("data-status");
			text_ = $(this).parent("div").prev("label").text();
			msg = "请选择" + text_.substring(0,text_.length-1)+'!'
			if(isEmpty(value) || value == 0 || value == -1) value = null;
		}else if(nodeName && nodeName.toLowerCase()=="textarea"){
			value = $(this).val();
			text_ = $(this).prev("label").text();
			msg = text_.substring(0,text_.length-1)+'不能为空!';
			 
		}else if(nodeName && nodeName.toLowerCase()=="select"){
			value = $(this).val();
			text_ = $(this).prev("label").text();
			msg = text_.substring(0,text_.length-1)+'不能为空!';
			if(isEmpty(value) || value == 0 || value == -1) value = null;
		}
		//console.log(msg);
		if(isEmpty(value)){
			 have_null = true;
			 LQWarn(msg);
			
			 return false;
		}
		if($(this).attr("data-minLen")){
			var len  = parseInt($(this).attr("data-minLen"));
			if(value.length<len){
				have_null = true;
			 	LQWarn( text_.substring(0,text_.length-1)+"长度不够");
			
			 return false;
			}
		}
		if($(this).attr("data-maxLen")){
			var len  = parseInt($(this).attr("data-minLen"));
			if(value.length>len){
				have_null = true;
			 	LQWarn( text_.substring(0,text_.length-1)+"长度超出限制");
			
			 return false;
			}
		}
		
	});

	
	
	return have_null;
}




function fillForm(obj,jsonValue){
	var nodeName=null;
	var attrName = null;
	obj.find("*").each(function(){
		if(!$(this).attr("name")){
			return true;
		}
		var type = $(this).attr("type");
		attrName = $(this).attr("name");
		nodeName = $(this).prop("nodeName");
		if(type && (type == "text" || type == "hidden")){
			$(this).val(jsonValue[attrName] || "");
			if($(this).hasClass("date")){
				var data_name = $(this).attr("data-name");
				$(this).val(formatToDate(jsonValue[data_name] || ""));
			}else if($(this).hasClass("date_time")){
				var data_name = $(this).attr("data-name")
				$(this).val(formatToDateTime(jsonValue[data_name] || ""));
			}
			
		}else if(nodeName && nodeName.toLowerCase()=="textarea" && !$(this).hasClass("html_editor")){
			
			$(this).val(jsonValue[attrName] || "");
			 
		}else if(nodeName && nodeName.toLowerCase()=="select"){

			$(this).val(jsonValue[attrName] || "0").trigger('change');
			if($(this).hasClass("text")){
				attrName = $(this).prev("input").attr("name");
				//console.log(attrName);
				var options = $(this).find("option"); // select下所有的option 
				var value_ = "";
				for(var i=0; i<options.length; i++) { 
					if(options.eq(i).text() == jsonValue[attrName]){
					//	console.log(options.eq(i).text() + "" + jsonValue[attrName]);
						value_ = options.eq(i).val();
					//	console.log(value_);
						$(this).val(value_).trigger('change');
						//$(this).trigger('change');
						return;
					}
				}
				
				
			/*	console.log($(this).find("option[text='"+ jsonValue[attrName]+"']").prop("nodeName"));
				console.log($(this).find("option[text='"+ jsonValue[attrName]+"']").eq(0).val());
				$(this).val($(this).find("option[text='"+ jsonValue[attrName]+"']").attr("value") || "0");*/
			}
		}else if(type && type == "checkbox"){
			var value_ = jsonValue[attrName] 
            if(!value_) return true;
            if(isArray(value_)){
                $.each(value_,function (i,dom){
                   obj.find(":checkbox[name='"+attrName+"'][value='"+dom+"']").prop("checked",true);
                });
                return true;
            }
            $(jsonValue[filedName].split(",")).each(function (i,dom){
                obj.find(":checkbox[name='"+attrName+"'][value='"+dom+"']").prop("checked",true);
            });
		}else if(type && type == "radio"){
       	 var value_ = jsonValue[attrName] ;
    	 console.log(value_);
    	$("input[name='"+attrName+"'][value='"+value_+"']").attr("checked",true);
    }
    else if($(this).hasClass("html_editor")){
			console.log(attrName);
			
			if (CKEDITOR.instances[attrName]){
				CKEDITOR.instances[attrName].destroy();
			}
			var data_name = $(this).attr("data-name");
			var editor = CKEDITOR.replace(attrName);
			//console.log(jsonValue[data_name] );
			console.log(data_name );
			editor.setData(jsonValue[attrName] ||jsonValue[data_name] );
		}
		
	});
	
	obj.find(".text_i").each(function(){
		
		attrName = $(this).attr("name");
		if(!attrName){
			attrName = $(this).nextAll("input[type='hidden']").eq(0).attr("name");
		}

		var html_name = "";
		$(this).nextAll("ul").find("a").each(function(){
			if($(this).attr("data-status") == jsonValue[attrName]){
				$(this).click();
				
				return false;
			}
		});
		/*$(this).html(html_name);
		$(this).attr("title",html_name);
		$(this).attr("data-status",jsonValue[attrName] || "0");*/
	});
	
	obj.find("img").each(function(){
		attrName = $(this).attr("name");
		if(attrName){
			$(this).attr("src",jsonValue[attrName] || "");
		}else{
			$(this).attr("src",$(this).nextAll("input[type='hidden']").eq(0).val());
		}
		
	});

	
}



	
function delObject(url,data_,msg){
	var msg_ = msg || "您确定删除吗？";
	LQConfirm(msg_,function(){
		$.ajax({
			url: url, 
			type: 'POST',
			data:data_,
			async: false,
			dataType: 'json', 
			timeout: 10000, 
			error: function(){LQWarn('请求失败,请稍后重试');}, 
			success: function(result){
				if(result.resultCode==1000){
					LQWarn("删除成功");
					qryList();
				}
				else if(result.resultCode==1004){
					LQWarn(result.returnObject);
					window.top.location.href ="../login.jsp";
				}else{
					LQWarn(result.returnObject);
				}
			}
		});	

	});				
}



function operateObject(url,data_,fun){
	$.ajax({
		url: url, 
		type: 'POST',
		data:data_,
		async: false,
		traditional:true,
		dataType: 'json', 
		timeout: 10000, 
		error: function(){LQWarn('请求失败,请稍后重试');}, 
		success: function(result){
			if(result.resultCode==1000){
				LQWarn("操作成功");
				qryList();
				if(fun){
					fun();
				}
				
			}
			else if(result.resultCode==1004){
				LQWarn(result.returnObject);
				window.top.location.href ="../login.jsp";
			}else{
				LQWarn(result.returnObject);
			}
		}
	});				
}




function getObjectWithFillData(url_,data_,formObj,fun){

	$.ajax({
		url: url_, 
		type: 'POST',
		data:data_,
		async: false,
		traditional:true,
		dataType: 'json', 
		timeout: 10000, 
		error: function(){LQWarn('请求失败,请稍后重试');}, 
		success: function(result){
			if(result.resultCode==1000){
				//console.log(result.returnObject);
				fillForm($(formObj),result.returnObject);
				if(fun){
					fun();
				}
				
			}
			else if(result.resultCode==1004){
				LQWarn(result.returnObject);
				window.top.location.href ="../login.jsp";
			}else{
				LQWarn(result.returnObject);
			}
		}
	});			
}

function common_ajax(url_,data_,fun){
	var result_ = null;
	$.ajax({
		url: url_, 
		type: 'POST',
		async: false,
		traditional:true,
		data:data_,
		dataType: 'json', 
		timeout: 10000, 
		error: function(){LQWarn('请求失败,请稍后重试');}, 
		success: function(result){
			console.log("======"+result);
			if(result.resultCode==1000){
				if(fun){
					eval(fun + "(result)");
				}
				//console.log(result);
				result_ =  result.returnObject;
			}
			/*else if(result.resultCode==1004){
				//LQWarn(result.returnObject);
				window.top.location.href ="../login_html.html";
			}*/else{
				/*alert(111)
				console.log(result.returnObject);
				LQWarn(result.resultCode);
				LQWarn(result.returnObject);
				LQWarn("111111111111"+result.returnObject+url_);*/
			}
			console.log(result);
		}
	});
	console.log(result_);
	return result_;
}

$(".prcie").keyup(function(){
	var obj = this;
	obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
	obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字而不是.
	obj.value = obj.value.replace(/\.{1,}/g,"."); //只保留第一个. 清除多余的 
	obj.value = obj.value.replace(/^(\d+)\.(\d{2}).*$/,'$1.$2'); //只能输入2个小数
});
$(".number").keyup(function(){
	var obj = this;
	obj.value = obj.value.replace(/[^\d]/g,""); //清除"数字"和"."以外的字符
	obj.value = obj.value.replace(/^0/g,""); //验证第一个字符是0而不是
});



function clearNoNum(obj){
	obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
	obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字而不是
	obj.value = obj.value.replace(/\.{1,}/g,"."); //只保留第一个. 清除多余的 
	obj.value = obj.value.replace(/^(\d+)\.(\d{2}).*$/,'$1.$2'); //只能输入2个小数
}

function returnFloat(value){
	 var value=Math.round(parseFloat(value)*100)/100;
	 var xsd=value.toString().split(".");
	 if(xsd.length==1){
	 	value=value.toString()+".00";
		 return value;
	 }
	 if(xsd.length>1){
	 	if(xsd[1].length<2){
	  		value=value.toString()+"0";
	 	}
	 	return value;
	}
	return value;
}

function serializeJsonObject(obj){
	var o = {};  
	var attrName = null;
	var value = null;
	var nodeName=null;
	obj.find("*").each(function(){
		attrName = $(this).attr("name");
		if(!attrName || attrName == "" ){
			return true;
		}
		if($(this).hasClass("text_i")){
			return true;
		}
		nodeName = $(this).prop("nodeName");
		
		value = $(this).val();
		if(nodeName && nodeName.toLowerCase()=="select" && $(this).hasClass("text")){
			value = $(this).find("option:selected").text();
		}
		 if (o[attrName]) {    
	           if (!o[attrName].push) {    
	               o[attrName] = [o[attrName]];    
	           }    
	           o[attrName].push( value || '');    
	       } else {    
	           o[attrName] = value || '';    
	       } 
	});
	return o; 
		
}

jQuery.prototype.serializeObject=function(){  
	var a,o,h,i,e;  
	a=this.serializeArray();  
	o={};  
	h=o.hasOwnProperty;  
	for(i=0;i<a.length;i++){  
		e=a[i];  
		if(!h.call(o,e.name)){  
			o[e.name]=e.value;  
		}  
	}  
 return o;  
};  


Date.prototype.format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1, //月份 
        "d+": this.getDate(), //日 
        "h+": this.getHours(), //小时 
        "m+": this.getMinutes(), //分 
        "s+": this.getSeconds(), //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}